<template>
  <div id="cart">
    <header>
      <span @click="returnTo">
        <i class="iconfont icon-fanhui"></i>
      </span>
      <span>购物车</span>
    </header>
    <div class="cart-address">
      请添加收货地址
      <i class="iconfont icon-arrow-right-bold"></i>
    </div>
    <div v-show="!status" class="cart-login" >
      <span>您可以在登录后同步已加入购物车中的商品</span>
      <span @click="toLogin">去登录</span>
      <i class="iconfont icon-arrow-right-bold"></i>
    </div>

    <ul class="cart-list">
      <li v-for="v in goodsList" :key="v.id">
        <div class="cart-list-left">
          <input type="checkbox" :checked="v.isChecked" @change="updateStatus(v.id,$event.target.checked)">
          <img :src="v.image" alt="">
        </div>
        <div class="cart-list-right">
          <div>
            {{ v.title }}
          </div>

          <div>
            <i class="iconfont icon-qianqian-"></i>
            <span>{{  v.price}}</span>
            <span class="cart-list-right-count">
              <span @click.stop="updateNumber('-',v.id)">- </span>
              <input type="number" :value="v.goodsNum" @change="updateNumber('#',v.id,$event.target.value)">
              <span @click.stop="updateNumber('+',v.id)"> +</span>
            </span>
          </div>
        </div>

      </li>
      <li v-show="goodsList.length">
        <div>
          <input style="zoom: 200%;" type="checkbox" :checked="allChecked" @change="changeAll($event.target.checked)">
          <span style="font-size: 1.5rem;">全选</span>
          <span @click="deleteGoods" style="margin-left: 22rem; color: red; font-size:1.5rem ;">删除</span>
        </div>
      </li>
    </ul>

    <div class="cart-count">
      <div>
        <span>总计：</span>
        <span>{{totalMoney }}</span>
        <span>元</span>
      </div>
      <div>去结算</div>
    </div>

    <MessageBox @message="getMessage" :tips="tips" ></MessageBox>
    <div style="height: 5rem;"></div>
  </div>
</template>
<script>
import {mapGetters} from 'vuex'
import MessageBox from '@/components/MessageBox/MessageBox'
import {throttle} from '@/utils/utils'
export default {
  name: 'SamCart',
  components: {
    MessageBox,
  },
  data() {
    return {
      // 登录用户的id
      status:'',
      // 控制消息框的显示与隐藏
      tips:false,
    }
  },
  mounted(){
    // 初始化登录信息
    const user = localStorage.getItem('status')
    if(user){
      this.status = user
    }
  },
  computed:{
    ...mapGetters('cart',['goodsList','userId']),
    // 全选按钮
    allChecked(){
      let status = true
      for(let i=0;i<this.goodsList.length;i++){
        if(this.goodsList[i].isChecked === false){
          status = false
          break
        }
      }
      return status
    },
    totalMoney(){
      let total = 0
      this.goodsList.forEach(v => {
        if(v.isChecked){
          total += v.goodsNum * v.price
        }
      });
      return total
    }
  },
  methods:{
    returnTo(){
      this.$router.back()
    },
    // 函数节流
    updateNumber:throttle(function(tag,id,num){
      num = num * 1
      const data = {
        id,
        tag,
        num
      }
      this.$store.commit('cart/SETNUMBER',data)
    },200),
    // 控制商品的选中与未选中状态
    updateStatus(id,status){
      this.$store.commit('cart/SETSTATUS',{id,status})
    },
    changeAll(status){
      this.$store.commit('cart/SETALLSTATUS',status)
    },
    deleteGoods(){
      // 控制消息框的显示与隐藏
      const res = this.goodsList.filter(v=>v.isChecked)
      if(res.length > 0){
        this.tips = true 
      }
           
    },
    // 拿到用户点击消息框的结果，并作出相应处理
    getMessage(tag){
      this.tips = false
      this.tag = tag
      if(tag === 'delete'){
        this.$store.commit('cart/DELETE')
      }
    },
    toLogin(){
      this.$router.push('login')
    }
  }
};
</script>
<style lang='less' scoped>
#cart {
  padding-top: 4rem;
  background-color: rgb(242, 242, 242);

  header {
    width: 37.5rem;
    line-height: 4rem;
    background-color: white;
    position: fixed;
    top: 0;

    .iconfont {
      font-size: 2rem;
    }

    span {
      margin-left: 1rem;
    }
  }

  .cart-address {
    width: 37.5rem;
    line-height: 4rem;
    background-color: white;
    text-align: center;
    font-size: 1.5rem;
    border-top: 0.1rem solid gray;

    .iconfont {
      font-size: 1.5rem;
      margin-left: 20rem;
    }
  }

  .cart-login {
    background-color: rgb(247, 229, 207);
    line-height: 2.5rem;
    font-size: 1.3rem;
    padding-left: 1rem;

    span {
      &:last-of-type {
        margin-left: 6rem;
      }
    }

    .iconfont {
      font-size: 1.3rem;
    }
  }

  .cart-list{
    margin-top: 1rem; 
    li{
      margin-left: 1rem;
      margin-right: 1rem;
      background-color: white;
      display: flex;
      justify-content: space-between;
      padding: 1rem;
      .cart-list-left{
        position: relative;
        flex-grow: 1;
        input{
          zoom: 200%;
          display: inline-block;
          position: absolute;
          top: 45%;
        }
        img{
          width: 7rem;
          height: 7rem;
          margin-left: 3rem;
        }
      }
      .cart-list-right{
        flex-grow: 7;
        margin-left: 2rem;
        input{
          width: 2rem;
          height: 1.5rem;
        }
        div{
          &:first-child{
            font-size: 1.5rem;
          }
          &:last-child{
            font-size: 1.3rem;
            margin-top: 3rem;
            font-weight: bold;
          }
        }
        .cart-list-right-count{
          margin-left: 10rem;
          input{
            height: 2rem;
            text-align: center;
            margin-left: 1rem;
            margin-right: 1rem;
            outline: none;
            border: none;
            background-color: rgb(235, 233, 233);
          }
        }
      }
    }
  }
  .cart-count{
    width: 37.5rem;
    line-height: 5rem;
    background-color: white;
    position: fixed;
    bottom: 0;
    display: flex;
    border-top: 0.1rem solid gainsboro;

    div{
      &:first-child{
        text-align: center;
        flex-grow: 7;
        span{
          &:first-child{
            font-size: 1.5rem;
          }
          &:nth-child(2){
            color: red;
            font-weight: bold;
          }
          &:last-child{
            font-weight: bold;
            margin-left: 2rem;
            font-size: 1.5rem;
          }
        }
      }
      &:last-child{
        flex-grow: 3;
        background-color: rgb(92, 189, 227);
        text-align: center;
      }
    }
  }
}
</style>